﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MySqlCode
{
    public partial class Connector : Form
    {
        public Connector()
        {
            InitializeComponent();
            ShowData();
        }

        private static Connector _connector;
        public static Connector GetInstance()
        {
            if (_connector == null) _connector = new Connector();
            return _connector;
        }

        private void btnClose_BtnClick(object sender, EventArgs e)
        {
            this.Close();
        }

        public void ShowData()
        {
            App.LodaData();
            flpDataView.Controls.Clear();
            foreach (var item in App.Datas)
            {
                var dit = new DataItem();
                dit.LabText = item.Name;
                dit.GUID = item.GUID;
                dit.Visible = true;
                dit.Click += Dit_Click;
                dit.DoubleClick += Dit_DoubleClick;
                flpDataView.Controls.Add(dit);
            }
            flpDataView.Visible = true;
        }

        private void Dit_DoubleClick(object sender, EventArgs e)
        {
            var dit = (DataItem)sender;
            dit.BackColor = Color.Gainsboro;
            App.Database = App.GetModel(dit.GUID);

            var db = new Handler.MySQLHelper(App.Database);
            var isSucc = db.TestOpen();
            if (isSucc)
            {
                this.Visible = false;
                new Main().Show();
            }
            else MessageBox.Show("连接失败", "警告");
        }

        private void Dit_Click(object sender, EventArgs e)
        {
            var cols = flpDataView.Controls.GetEnumerator();
            while (cols.MoveNext())
            {
                ((DataItem)cols.Current).BackColor = Color.WhiteSmoke;
            }

            var dit = (DataItem)sender;
            dit.BackColor = Color.Gainsboro;
            App.Database = App.GetModel(dit.GUID);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Add.GetInstance().Mode = 1;
            Add.GetInstance().GUID = "";
            Add.GetInstance().txtName.Text = "";
            Add.GetInstance().txtHost.Text = "";
            Add.GetInstance().txtPort.Text = "3306";
            Add.GetInstance().txtPwd.Text = "";
            Add.GetInstance().txtUser.Text = "root";
            Add.GetInstance().ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (App.Database == null) MessageBox.Show("请选择一个连接", "警告");
            else
            {
                Add.GetInstance().Mode = 2;
                Add.GetInstance().SetModel(App.Database);
                Add.GetInstance().ShowDialog();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (App.Database == null) MessageBox.Show("请选择一个连接", "警告");
            else
            {
                if (MessageBox.Show("注意：删除此项将不可恢复", "是否删除？", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    App.Delete(App.Database);
                    App.Database = null;
                    ShowData();
                }
            }
        }

        private void flpDataView_Click(object sender, EventArgs e)
        {
            var cols = flpDataView.Controls.GetEnumerator();
            while (cols.MoveNext())
            {
                ((DataItem)cols.Current).BackColor = Color.WhiteSmoke;
            }
            App.Database = null;
        }
    }
}
